#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"
#include "project.h"
#include "database.h"

/*
* Compile options:
*   gcc -I/usr/include/mysql -L/usr/lib/mysql -o mysql_sample mysql_sample.c -lmysqlclient -lz
*
*/

/* global definition */
#define CONN_HOST "192.168.3.102"
#define CONN_USER "root"
#define CONN_PASS "666666"
#define CONN_DB   "weipedigree"
#define CONN_TABLE   "unit"

/* global MySQL handler */
MYSQL mysql;
int is_init_db = 0;
char sql_buf[512];

void
exiterr(int exitcode)
{
	fprintf(stderr, "%s ", mysql_error(&mysql));
	exit(exitcode);
}

int
init_db()
{
	//printf("init_db() begin\n");
	MYSQL_RES *res;
	MYSQL_ROW row;
	uint i = 0;

	/* 1. init MySQL handler */
	if (!mysql_init(&mysql))
	{
		exiterr(-1);
	}

	/* 2. connect to MySQL */
	if (!mysql_real_connect
	    (&mysql, CONN_HOST, CONN_USER, CONN_PASS, NULL, MYSQL_PORT, NULL,
	     0))
	{
		exiterr(-2);
	}

	/* 3. select Database */
	if (mysql_select_db(&mysql, CONN_DB))
	{
		exiterr(-3);
	}
#if 0
	/* 4. excute a query */
	char *sqlstr = "SELECT * FROM unit";
	if (mysql_query(&mysql, sqlstr))
	{
		exiterr(-4);
	}

	/* 5. store result */
	if (!(res = mysql_store_result(&mysql)))
	{
		exiterr(-5);
	}

	/* 6. fetch row and get the result */
	while ((row = mysql_fetch_row(res)))
	{
		for (i = 0; i < mysql_num_fields(res); i++)
		{
			//printf("%s\t", row[i]);
		}
		//printf("\n");
	}

	/* 7. free result */
	mysql_free_result(res);

	/* 8. close MySQL Connection */
	mysql_close(&mysql);
#endif
	//printf("init_db() end\n");
	return 0;
}

#if 0 //debug
int
main()
{
	//printf("main begin\n");
	struct pedigree_list_unit list_unit;
	int ret;
	char name[20];

	//printf("main 1\n");
	ret = get_data(1, &list_unit);

	//printf("%d:%s\n", list_unit.self.id, list_unit.self.name);

	//init_db();

	get_name(2, name);
	//printf("name:%s\n", name);
	//printf("main end\n");
	return 0;
}

#endif

char* get_name(int id)
{
	char *name;
	if (is_init_db == 0) {
		init_db();
		is_init_db = 1;
	}

	MYSQL_RES *res;
	MYSQL_ROW row;
	
	sprintf(sql_buf, "SELECT name FROM %s where id=%d", CONN_TABLE, id);

	if (mysql_query(&mysql, sql_buf))
	{
		return NULL;
	}

	if (!(res = mysql_store_result(&mysql)))
	{
		return NULL;
	}

	if (!(row = mysql_fetch_row(res)))
	{
		return NULL;
	}

	name = row[0];

	mysql_free_result(res);


	return name;
}

int get_sex(int id)
{
	int sex;
	if (is_init_db == 0) {
		init_db();
		is_init_db = 1;
	}

	MYSQL_RES *res;
	MYSQL_ROW row;
	
	sprintf(sql_buf, "SELECT sex FROM %s where id=%d", CONN_TABLE, id);

	if (mysql_query(&mysql, sql_buf))
	{
		return -1;
	}

	if (!(res = mysql_store_result(&mysql)))
	{
		return -1;
	}

	if (!(row = mysql_fetch_row(res)))
	{
		return -1;
	}

	sex = atoi(row[0]);

	mysql_free_result(res);


	return sex;
}

#if 1
int get_data(int id, struct pedigree_list_unit *list_unit)
{
	if (is_init_db == 0) {
		init_db();
		is_init_db = 1;
	}

	MYSQL_RES *res;
	MYSQL_ROW row;
	struct pedigree_list_unit unit;
	char *buf, *tmp;
	int i, wife_id[20], children_id[20];
	
	sprintf(sql_buf, "SELECT * FROM %s where id=%d", CONN_TABLE, id);

	if (mysql_query(&mysql, sql_buf))
	{
		return -1;
	}

	if (!(res = mysql_store_result(&mysql)))
	{
		return -1;
	}

	if (!(row = mysql_fetch_row(res)))
	{
		return -1;
	}


	list_unit->self.id = atoi(row[0]);
	list_unit->self.name = row[1];
	
	buf = row[3];
	tmp = strtok(buf, ",");
	i = 0;
	while(tmp) {
		wife_id[i] = atoi(tmp);
		tmp = strtok(NULL, ",");
		i++;
	}
	list_unit->wife_num = i;
	list_unit->wife = (struct pedigree_unit *) malloc(i * sizeof (struct pedigree_unit));
	for (i = 0; i < list_unit->wife_num; i++) {
		list_unit->wife[i].id = wife_id[i];
		if (get_name(list_unit->wife[i].id) == NULL) {
			list_unit->wife[i].name = NULL;
		}else {
			list_unit->wife[i].name = strdup(get_name(list_unit->wife[i].id));
		}
	}
	
	list_unit->father.id = atoi(row[4]);
	if (get_name(list_unit->father.id) == NULL) {
		list_unit->father.name = NULL;
	}else {
		list_unit->father.name = strdup(get_name(list_unit->father.id));
	}
	
	list_unit->mother.id = atoi(row[5]);
	if (get_name(list_unit->mother.id) == NULL) {
		list_unit->mother.name = NULL;
	}else {
		list_unit->mother.name = strdup(get_name(list_unit->mother.id));
	}

	buf = row[6];
	tmp = strtok(buf, ",");
	i = 0;
	while(tmp) {
		children_id[i] = atoi(tmp);
		tmp = strtok(NULL, ",");
		i++;
	}
	list_unit->children_num = i;
	list_unit->children = (struct pedigree_unit *) malloc(i * sizeof (struct pedigree_unit));
	for (i = 0; i < list_unit->children_num; i++) {
		list_unit->children[i].id = children_id[i];
		if (get_name(list_unit->children[i].id) == NULL) {
			list_unit->children[i].name = NULL;
		}else {
			list_unit->children[i].name = strdup(get_name(list_unit->children[i].id));
		}
		list_unit->children[i].sex = get_sex(list_unit->children[i].id);
	}
	
	list_unit->dis = row[7];

	mysql_free_result(res);

	return 0;
}

#else

int
get_data(int id, struct pedigree_list_unit *p_l_unit)
{
	struct pedigree_list_unit p[3];

	p[0].self.id = 1;
	p[0].self.name = strdup("root");

	p[0].wife =
	    (struct pedigree_unit *) malloc(1 * sizeof (struct pedigree_unit));
	p[0].wife[0].id = 101;
	p[0].wife[0].name = strdup("rootwife");
	p[0].wife_num = 1;

	p[0].father.id = -1;
	p[0].father.name = NULL;

	p[0].mother.id = -1;
	p[0].mother.name = NULL;

	p[0].children =
	    (struct pedigree_children_unit *) malloc(2 *
						     sizeof (struct
							     pedigree_children_unit));
	p[0].children[0].id = 2;
	p[0].children[0].name = strdup("child1");
	p[0].children[0].sex = 1;
	p[0].children[1].id = 3;
	p[0].children[1].name = strdup("child2");
	p[0].children[1].sex = 0;
	p[0].children_num = 2;

	p[0].dis = strdup("discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........discription root..........");

	p[1].self.id = 2;
	p[1].self.name = strdup("child1");

	p[1].wife =
	    (struct pedigree_unit *) malloc(2 * sizeof (struct pedigree_unit));
	p[1].wife[0].id = 102;
	p[1].wife[0].name = strdup("child1wife1");
	p[1].wife[1].id = 1022;
	p[1].wife[1].name = strdup("child1wife2");
	p[1].wife_num = 2;

	p[1].father.id = 1;
	p[1].father.name = strdup("root");

	p[1].mother.id = 101;
	p[1].mother.name = strdup("rootwife");

	p[1].children =
	    (struct pedigree_children_unit *) malloc(2 *
						     sizeof (struct
							     pedigree_children_unit));
	p[1].children[0].id = 4;
	p[1].children[0].name = strdup("child3");
	p[1].children[0].sex = 1;
	p[1].children[1].id = 5;
	p[1].children[1].name = strdup("child4");
	p[1].children[1].sex = 0;
	p[1].children_num = 2;

	p[1].dis = strdup("discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........discription child1..........");

	p[2].self.id = 3;
	p[2].self.name = strdup("child2");

	p[2].wife =
	    (struct pedigree_unit *) malloc(1 * sizeof (struct pedigree_unit));
	p[2].wife[0].id = 103;
	p[2].wife[0].name = strdup("child2wife");
	p[2].wife_num = 1;

	p[2].father.id = 1;
	p[2].father.name = strdup("root");

	p[2].mother.id = 101;
	p[2].mother.name = strdup("rootwife");

	p[2].children =
	    (struct pedigree_children_unit *) malloc(2 *
						     sizeof (struct
							     pedigree_children_unit));
	p[2].children[0].id = 6;
	p[2].children[0].name = strdup("child5");
	p[2].children[0].sex = 1;
	p[2].children[1].id = 7;
	p[2].children[1].name = strdup("child6");
	p[2].children[1].sex = 0;
	p[2].children_num = 2;

	p[2].dis = strdup("discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........discription child2..........");

	if (id == 1)
		memcpy(p_l_unit, &p[0], sizeof (p[0]));	//p = p[0];
	else if (id == 2)
		memcpy(p_l_unit, &p[1], sizeof (p[1]));	//p = p[1];
	else if (id == 3)
		memcpy(p_l_unit, &p[2], sizeof (p[2]));	//p = p[2];
	else {
		p_l_unit = NULL;
		return -1;
	}
	return 0;

}
#endif

